package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
      //查看当前菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentThread());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        //如果已经存在num+1,操作cart
        if(cart != null){
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }
        else {
            //不存在则添加,保存shopping_cart
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentThread());
            shoppingCartService.save(shoppingCart);
        }


        return R.success("添加购物车成功!");
    }

    /*
    *   查询购物车
    * */
 @GetMapping("list")
    public R<List<ShoppingCart>> list(){
     LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
             .eq(ShoppingCart::getUserId, BaseContext.getCurrentThread());
     List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
     return R.success(list);
 }

    /*
    *  清空购物车
    * */

    @DeleteMapping("clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentThread());
        shoppingCartService.remove(queryWrapper);
        return R.success("购物车已清空");
    }

    /*
     *  减少购物车中菜品的数量  sub
     * */
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentThread())
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart.getNumber()==1) {
            shoppingCartService.remove(queryWrapper);
        }
        else{
            cart.setNumber(cart.getNumber()-1);
            shoppingCartService.updateById(cart);
        }

        return  R.success("减少成功");
    }
}
